<?php

namespace App\Shop\Controllers;

use App\Http\Controllers\BaseController;
use App\Models\AgentUser;
use App\Models\CashOrder;
use App\Models\GoodOrder;
use App\Models\GoodOrderDetail;
use App\Models\PaymentOrder;
use App\Models\ServiceOrder;
use App\Models\ShopUser;
use App\Models\User;
use App\Models\Worker;
use App\Shop\Forms\Cash\HandleCashOrder;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class BasicController extends BaseController
{
    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title("数据看板")
            ->body(function (Row $row){

                $admin = Admin::user();
                $isShop = $admin->isAdministrator();
                if ($isShop){
                    $shop_id = $admin->id;
                }else{
                    $shop_id = $admin->shop_id;
                }
                $data['shop'] = $admin;
                $data['cash_money'] = CashOrder::query()->where("shop_id",$shop_id)->where("type",0)->where("status",0)->sum("amount");
                $data['shop']['money'] = $data['shop']['money'] - $data['cash_money'];
                $data['shop']['used_money'] = floatval($data['shop']['used_money']);
                $orderPrice = ServiceOrder::query()->where("shop_id",$shop_id)->where("status","<>",4)->sum("amount");
                $orderPrice += GoodOrder::query()->where("shop_id",$shop_id)->whereIn("state",[1,2,3,4])->sum("amount");
                $data['orderPrice'] = $orderPrice;
                $tab = Tab::make();
                $data['modal'] = Modal::make()->lg()
                   ->title('提现申请')
                   ->body(HandleCashOrder::make())->id("modal-cash");
                $tab->add('基础数据', view('shop.basic.index',$data), true);
                $row->column(12,$tab->withCard());
            });
    }

    protected function getWhereRange($field = "created_at"): \Closure
    {
        return function ($query) use ($field) {
            $type = request()->post('type');
            $range = request()->post('range');
            if ($type=='custom'){
                $query->when($range,function ($q,$value){
                    $value[0] = Carbon::parse($value[0]/1000)->format("Y-m-d 00:00:00");
                    $value[1] = Carbon::parse($value[1]/1000)->format("Y-m-d 23:59:59");
                    $q->whereBetween("created_at",$value);
                });
            }else{
                $carbon = now();
                switch ($type){
                    case "yesterday":
                        $d = $carbon->addDays(-1);
                        $range[0] = $d->startOfDay()->format("Y-m-d 00:00:00");
                        $range[1] = $d->endOfDay()->format("Y-m-d 23:59:59");
                        break;
                    case "today":
                        $range[0] = $carbon->startOfDay()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfDay()->format("Y-m-d 23:59:59");
                        break;
                    case "week":
                        $range[0] = $carbon->startOfWeek()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfWeek()->format("Y-m-d 23:59:59");
                        break;
                    case "month":
                        $range[0] = $carbon->startOfMonth()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfMonth()->format("Y-m-d 23:59:59");
                        break;
                    case "quarter":
                        $range[0] = $carbon->startOfQuarter()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfQuarter()->format("Y-m-d 23:59:59");
                        break;
                    case "year":
                        $range[0] = $carbon->startOfYear()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfYear()->format("Y-m-d 23:59:59");
                        break;
                    default:
                        $range = [];
                        break;
                }
                if ($range){
                    $query->whereBetween($field,$range);
                }
            }
        };
    }

    protected function range()
    {
        $whereRange = $this->getWhereRange();
        $admin = Admin::user();
        $isShop = $admin->isAdministrator();
        if ($isShop){
            $shop_id = $admin->id;
        }else{
            $shop_id = $admin->shop_id;
        }
        $data['items'] = [
            ['label'=>'洗车交易量','value'=>ServiceOrder::query()->where("shop_id",$shop_id)->where($whereRange)->where("status","<>",4)->count()],
            ['label'=>'洗车交易额','value'=>ServiceOrder::query()->where("shop_id",$shop_id)->where($whereRange)->where("status","<>",4)->sum("amount")],
            ['label'=>'商城交易量','value'=>GoodOrder::query()->where("shop_id",$shop_id)->where($whereRange)->whereIn("state",[1,2,3,4])->count()],
            ['label'=>'商城交易额','value'=>GoodOrder::query()->where("shop_id",$shop_id)->where($whereRange)->whereIn("state",[1,2,3,4])->sum("amount")],
            ['label'=>'会员卡交易量','value'=>PaymentOrder::query()->where("shop_id",$shop_id)->where("type",2)->where($whereRange)->where("pay_status",1)->count()],
            ['label'=>'会员卡交易额','value'=>PaymentOrder::query()->where("shop_id",$shop_id)->where("type",2)->where($whereRange)->where("pay_status",1)->sum("amount")],
        ];
        return $this->response()->data($data);
    }


    public function cash(){
        $shopUser = auth("shop")->user();
        CashOrder::cash(0,$shopUser);
    }
}
